<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/12
 * Time: 10:43
 */
namespace Service\Controller;
use Admin\Controller\AdminController;

class OrderController extends AdminController
{
    protected $modelName = 'Order';

    public function lst(){
        $req = I('param.');
        $tel = $req['tel'];
        $where['attestation_tel'] = $tel;
        $where['tel'] = $tel;
        $where['_logic'] = 'or';
        $len = strlen($tel);
        if($len == 11)
            $data_tel = D('Customer')->where($where)->relation(array('families','orders'))->select();
        else
            $data_tel = D('Customer')->where(array('phone'=>$tel))->relation(array('families','orders'))->select();
        $data_contact = D('Customer')->where(array('contact'=>array('like','%'.$tel.'%')))->relation(array('families','orders'))->limit(10)->select();
        if($data_tel){
            $data_tel[0]['primary'] = 1;
        }
        $model = D('WxOrder');
        $count['1'] = $model->countStep(1);
        $count['2'] = $model->countStep('3,4');
        $this->assign('count', $count);
        $communityData = M('Community')->where(['children_count'=>array('gt',0)])->select();
        $this->assign('communityData', $communityData);

        $data = D('Order')->getLst();
        $this->assign('user',getUser());
        $this->assign('data',$data['data']);
        $this->assign('page',$data['page']);
        $this->display();
    }

    public function handlerLst(){
        $communityData = M('Community')->where(['children_count'=>array('gt',0)])->select();
        $this->assign('communityData', $communityData);

        $data = D('Service/Order')->getLst();
        $this->assign('data',$data['data']);
        $this->assign('page',$data['page']);
        $this->display();
    }

    public function appoint(){
//        $data = M('User')->alias('a')->field('a.*,c.name as role_name')->join('left join zh_role_user b on a.id=b.user_id left join zh_role c on b.role_id=c.id')->where(['c.id'=>4])->select();
//        dd($data);
//        $this->assign('data',$data);
        $this->display();
    }

    public function getCommunity(){
        $data = M('Community')->select();
        $this->ajaxReturn($data);
    }

    public function getAssistant(){
        $community_id  = I('community_id');
        $community_name = M('Community')->find($community_id)['name'];
        $map['a.PRIV_NAME'] = array('like',"%$community_name%");
        $map['a.DEPT_NAME'] = array('like',"%$community_name%");
        $map['a.assistant_map'] = array('like',"%$community_id%");
        $map['_logic'] = 'OR';
        $where['c.id'] = 4;
        $where['a.is_active'] = 1;
        $where['_complex'] = $map;
        $data = M('User')->alias('a')->field('a.*,c.name as role_name')->join('left join zh_role_user b on a.id=b.user_id left join zh_role c on b.role_id=c.id')->where($where)->select();
        $this->ajaxReturn($data);
    }

    public function appointing(){
        $res = D('Order')->appoint();
        $this->ajaxReturn($res);
    }

    public function done(){
        $res = D('Order')->done();
        $this->ajaxReturn($res);
    }
    public function unDone(){
        $res = D('Order')->unDone();
        $this->ajaxReturn($res);
    }

    public function doneMsg(){
        $id = I('id');
        $this->assign('id',$id);
        $this->display();
    }
    public function unDoneMsg(){
        $id = I('id');
        $this->assign('id',$id);
        $this->display();
    }
    public function off(){
        $id = I('id');
        $this->assign('id',$id);
        $this->display();
    }
    public function offing(){
        $res = D('Order')->offing();
        $this->ajaxReturn($res);
    }

    public function dumpExcel(){
        $start_time = I('start_time');
        $end_time = I('end_time');
        if($start_time)
            $time['create_time'] = array('GT',strtotime($start_time));
        if($end_time)
            $time['create_time'] = array('LT',strtotime($end_time));
        $param = $this->getParam();
        $param = array_merge($param,$time);
        $modelName = $this->modelName;
        $scope = array();
        $relation = array();
        $search = $this->getSearch();
//        myDump($search);
//        myDump($param);die;
        $orderData = D($modelName)->where($param)->where($search)->scope($scope)->relation($relation)->order('create_time desc')->select();
        foreach ($orderData as $k=>$v) {
            $data[$k][] = $v['DATA_9'];
            $data[$k][] = str_replace("\r","-",str_replace("\n","-",$v['DATA_10']));
            $data[$k][] = $v['DATA_6'];
            $data[$k][] = $v['DATA_8'];
            $data[$k][] = $v['DATA_11'];
            $data[$k][] = str_replace("\r","-",str_replace("\n","-",$v['DATA_12']));
            $data[$k][] = $v['create_time'];
            $data[$k][] = $v['DATA_1'];
//            $_data = $this->prcsInfo($v['ID']);
//            foreach ($_data as $v1) {
//                $data[$k][] = $v1;
//            }
        }
        $title = ['工单大类','工单细类','业主姓名','业主地址','业主电话','事件说明','创建时间','创建人','事件动态'];
        dumpExcel($title,$data,'报修记录');
//        myDump($data);
    }
}